package com.minio.view;

import com.minio.pojo.MinioData;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.service.ResponseMessage;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @auther LiZiBa
 * @date 2020/11/22 12:35
 * @description:
 **/

@Api(tags = {"web服务：minio上传、下载、删除接口"})
@RestController
@RequestMapping("view/minio")

@CrossOrigin(origins = {"http://127.0.0.1:8090", "null"})
public class MinioView {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioData minioData;

    /**
     * 文件下载
     * @param fileName
     * @param response
     */
    @ApiOperation(value = "文件下载")
    @GetMapping(value = "/download")
    @SneakyThrows(Exception.class)
    public void download(@RequestParam("fileName")String fileName, HttpServletResponse response) {
        InputStream in = null;
        ObjectStat stat = minioClient.statObject(minioData.getBucketName(), fileName);
        response.setContentType(stat.contentType());
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        in = minioClient.getObject(minioData.getBucketName(), fileName);
        IOUtils.copy(in, response.getOutputStream());
        in.close();
    }

    /**
     * 文件上传成功
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/upload")
    @SneakyThrows(Exception.class)
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RuntimeException("上传文件不能为空");
        } else {
            // 得到文件流
            final InputStream is = file.getInputStream();
            // 文件名
            final String fileName = file.getOriginalFilename();
            // 把文件放到minio的boots桶里面
            minioClient.putObject(minioData.getBucketName(), fileName, is, new PutObjectOptions(is.available(), -1));
            // 关闭输入流
            is.close();
            return "上传成功";
        }
    }


    /**
     * 删除文件
     * @param fileName
     * @return
     */
    @ApiOperation(value = "删除文件")
    @GetMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SneakyThrows(Exception.class)
    public String delete(@RequestParam("fileName") String fileName) {
        minioClient.removeObject(minioData.getBucketName(), fileName);
        return "删除成功";
    }


}
